﻿using Microsoft.Ajax.Utilities;
using System.Reflection;
using System.Web.Mvc;
using Zhang.AdminPage;
using Zhang.Model;

namespace Zhang.Admin.Controllers
{
    public class NotesController : BaseController
    {
        // GET: Notes
        public ActionResult Index(Model.UCenter.NotesSch schModel=null)
        {
            var pgIndex = PageIndex;
            pgIndex = pgIndex < 1 ? 1 : pgIndex;
            var result = new BLL.UCenter.Notes.Notes().SelectList(PageSize, PageIndex, schModel);
            var totalRS = result.Item2;
            var pgCount = totalRS / PageSize;
            pgCount = pgCount * PageSize < totalRS ? pgCount + 1 : pgCount;
            ViewBag.RecommendPos = "";
            if (totalRS > PageSize)
            {
                ViewBag.SplitHtml = Utils.SplitPage.GetHtmlCode("/Notes/", 6, pgIndex, PageSize, totalRS);
            }
            ViewBag.schModel = schModel;

            return View(result.Item1);
        }

        #region 添加
        public ActionResult Add()
        {
            return View();
        }

        [HttpPost]
        [ValidateInput(false)]
        public ActionResult Add(Model.UCenter.Notes model)
        {
            MessageType msgType;
            var id = new BLL.UCenter.Notes.Notes().Insert(model);

            if (id > 0)
            {
                MessageContent = "恭喜您，操作成功";
                msgType = Model.MessageType.Success;
            }
            else
            {
                msgType = Model.MessageType.Failure;
            }

            NoDisplay(msgType);
            return null;
        }


        #endregion

        #region 编辑
        /// <summary>
        /// 编辑
        /// </summary>
        /// <returns></returns>
        public ActionResult Edit(int id)
        {
            Model.MessageType msgType;
            var model = new BLL.UCenter.Notes.Notes().Select(id);

            if (model == null)
            {
                msgType = Model.MessageType.Attention;
                MessageContent = "当前记录不存在（或是已删除）";
                ShowMessage(msgType);
            }
            return View();
        }

        /// <summary>
        /// 编辑 提交
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [ValidateInput(false)]
        public ActionResult Edit(Model.UCenter.Notes model)
        {
            MessageType msgType;
            var isSuccess = new BLL.UCenter.Notes.Notes().Update(model);

            if (isSuccess)
            {
                MessageContent = "恭喜您，操作成功";
                msgType = Model.MessageType.Success;
            }
            else
            {
                msgType = Model.MessageType.Failure;
            }

            NoDisplay(msgType);
            return null;
        }


        #endregion


        //返回链接
        private void ListSetUrlLink()
        {
            DefaultBackLink = new Model.Link { Title = "返回列表页", URL = "/Notes?" + Request["backUrlParam"] };
        }
        /// <summary>
        /// 正确不显示
        /// </summary>
        /// <returns></returns>
        public void NoDisplay(Model.MessageType msgType)
        {
            ListSetUrlLink();
            if (msgType != Model.MessageType.Success)
            {
                ShowMessage(msgType);
            }
            else
            {
                Response.Redirect(DefaultBackLink.URL);
            }

        }
    }
}